using System;
using System.Net;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using Engine;
using Engine.Input;

namespace Game
{
	public class LJMainMenuScreen : Screen
	{
		private string m_versionString = string.Empty;

		private string m_serverInfo = string.Empty;

		private bool m_versionStringTrial;

		public LJMainMenuScreen()
		{
			XElement xElement = ModUtils.LoadXmlFromAssembly(this, "LJMainMenuScreen.xml");
			WidgetsManager.LoadWidgetContents((Widget)this, (object)this, xElement);
		}

		public override void Enter(object[] parameters)
		{
			Children.Find<MotdWidget>(true).Restart();
			if (SettingsManager.IsolatedStorageMigrationCounter < 3)
			{
				SettingsManager.IsolatedStorageMigrationCounter++;
				VersionConverter126To127.MigrateDataFromIsolatedStorageWithDialog();
			}
		}

		public override void Leave()
		{
			Keyboard.BackButtonQuitsApp = false;
		}

		public override void Update()
		{
			Keyboard.BackButtonQuitsApp = !MarketplaceManager.IsTrialMode;
			if (string.IsNullOrEmpty(m_versionString) || MarketplaceManager.IsTrialMode != m_versionStringTrial)
			{
				m_versionString = string.Format("Version {0}{1}", VersionsManager.Version, MarketplaceManager.IsTrialMode ? " (Demo)" : string.Empty);
				m_versionStringTrial = MarketplaceManager.IsTrialMode;
			}
			if (string.IsNullOrEmpty(m_serverInfo))
			{
				if (NetworkManager.Manager.IsRunning)
				{
					IPAddress localAddress = NetworkManager.LocalAddress;
					m_serverInfo = string.Format("Server started on " + ((localAddress != null) ? localAddress.ToString() : null));
				}
				else
				{
					m_serverInfo = "Server failed to start, check logs";
				}
			}
			Children.Find("MultiPlay").IsVisible = NetworkManager.Manager.IsRunning;
			Children.Find("Buy").IsVisible = MarketplaceManager.IsTrialMode;
			Children.Find<LabelWidget>("Version").Text = m_versionString;
			Children.Find<LabelWidget>("ServerInfo").Text = m_serverInfo;
			RectangleWidget rectangleWidget = Children.Find<RectangleWidget>("Logo");
			float num = 1f + 0.02f * MathUtils.Sin(1.5f * (float)MathUtils.Remainder(Time.FrameStartTime, 10000.0));
			rectangleWidget.RenderTransform = Matrix.CreateTranslation((0f - rectangleWidget.ActualSize.X) / 2f, (0f - rectangleWidget.ActualSize.Y) / 2f, 0f) * Matrix.CreateScale(num, num, 1f) * Matrix.CreateTranslation(rectangleWidget.ActualSize.X / 2f, rectangleWidget.ActualSize.Y / 2f, 0f);
			if (Children.Find<ButtonWidget>("Play").IsClicked)
			{
				ScreensManager.SwitchScreen("Play");
			}
			if (Children.Find<ButtonWidget>("MultiPlay").IsClicked)
			{
				DialogsManager.ShowDialog(this, new TextBoxDialog("Enter Server IP", "", 60, new Action<string>(_003CUpdate_003Eb__6_0)));
			}
			if (Children.Find<ButtonWidget>("Help").IsClicked)
			{
				ScreensManager.SwitchScreen("Help");
			}
			if (Children.Find<ButtonWidget>("Content").IsClicked)
			{
				ScreensManager.SwitchScreen("Content");
			}
			if (Children.Find<ButtonWidget>("Settings").IsClicked)
			{
				ScreensManager.SwitchScreen("Settings");
			}
			if ((base.Input.Back && !Keyboard.BackButtonQuitsApp) || base.Input.IsKeyDownOnce(Key.Escape))
			{
				if (MarketplaceManager.IsTrialMode)
				{
					ScreensManager.SwitchScreen("Nag");
				}
				else
				{
					Window.Close();
				}
			}
		}

		[CompilerGenerated]
		private void _003CUpdate_003Eb__6_0(string str)
		{
			if (!string.IsNullOrEmpty(str))
			{
				try
				{
					ScreensManager.SwitchScreen("LJLoading", new IPEndPoint(IPAddress.Parse(str), 50550));
				}
				catch (Exception message)
				{
					Log.Error(message);
					DialogsManager.ShowDialog(this, new MessageDialog("Error", "Please enter with the format <ip address>:<port>", "OK", null, null));
				}
			}
		}
	}
}
